package com.xiongwei.myspringbootproject.web.controller;

import com.xiongwei.myspringbootproject.dao.ProductJDDao;
import com.xiongwei.myspringbootproject.dao.ProductZKHDao;
import com.xiongwei.myspringbootproject.dao.RelationDao;
import com.xiongwei.myspringbootproject.domain.Product;
import com.xiongwei.myspringbootproject.domain.ProductZKH;
import com.xiongwei.myspringbootproject.domain.jd_zkh_relation;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author XiongWei
 * @date 2021/9/28
 */
@RestController
public class TestController {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    private ProductJDDao productJDDao;

    @Autowired
    private ProductZKHDao productZKHDao;

    @Autowired
    private RelationDao relationDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/test")
    public void test() {
        System.out.println("here");
    }

    @GetMapping("/startMatching")
    public void startMatching() {
//        Query queryZKH = entityManager.createNativeQuery("select a.id, a.code, a.name, a.spec, b.name as brand from product_sku a left join category b on a.brand_id = b.id where a.company_id = 426", ProductSQL.class);
//        List<ProductSQL> zkhList= queryZKH.getResultList();
        List<ProductZKH> zkhList = productZKHDao.findAll();
        //遍历震坤行数据匹配jd
        zkhList.forEach(zkhProduct -> {
            NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
            BoolQueryBuilder rootQueryBuilder = QueryBuilders.boolQuery();

            String zkhName = zkhProduct.getName();
            if (StringUtils.hasText(zkhName)) {
                rootQueryBuilder.must(QueryBuilders.matchQuery("name", zkhName));
            }

            String zkhBrand = zkhProduct.getBrand();
            if (StringUtils.hasText(zkhBrand)) {
                rootQueryBuilder.must(QueryBuilders.matchQuery("brand", zkhBrand));
            }

            String zkhSpec = zkhProduct.getSpec();
            if (StringUtils.hasText(zkhSpec)) {
                rootQueryBuilder.must(QueryBuilders.matchQuery("spec", zkhSpec));
            }

            nativeSearchQueryBuilder.withQuery(rootQueryBuilder);
            //获取匹配结果
            SearchHits<Product> searchResult = elasticsearchRestTemplate.search(nativeSearchQueryBuilder.build(), Product.class);
            if (searchResult.hasSearchHits()) {
                List<SearchHit<Product>> searchHits = searchResult.getSearchHits();
                if (!CollectionUtils.isEmpty(searchHits)) {
                    List<jd_zkh_relation> relationList = new ArrayList();
                    searchHits.forEach(productJD -> {
                        jd_zkh_relation relation = new jd_zkh_relation();
                        relation.setJdCode(productJD.getContent().getCode());
                        relation.setZkhCode(zkhProduct.getCode());
                        relation.setJdName(productJD.getContent().getName());
                        relation.setZkhName(zkhProduct.getName());
                        relation.setJdBrand(productJD.getContent().getBrand());
                        relation.setZkhBrand(zkhProduct.getBrand());
                        relation.setJdSpec(productJD.getContent().getSpec());
                        relation.setZkhSpec(zkhProduct.getSpec());
                        relationList.add(relation);
                    });
                    if (!CollectionUtils.isEmpty(relationList)) {
                        relationDao.saveAll(relationList);
                    }
                }

            }
        });
        System.out.println("匹配完成");
    }
}
